package com.hdoit.kylin.logical.proposal.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.DaoBaseException;
import com.tennetcn.common.utils.CommonUtils;
import com.hdoit.kylin.logical.proposal.dao.IProposalDao;
import com.hdoit.kylin.logical.proposal.model.Proposal;
import com.hdoit.kylin.logical.proposal.service.IProposalService;
import com.hdoit.kylin.logical.relative.model.ProposalDocument;
import com.hdoit.kylin.logical.relative.service.IProposalDocumentService;
import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.ProposalSearch;
/**
 * @author      tanzulie
 * @email       
 * @createtime  2017年08月01日 14:54:35
 * @comment
 */
@Component
public class ProposalServiceImpl extends SuperService<Proposal> implements IProposalService{

	private IProposalDao proposalDao;
	private ITeacherService teacherService;
	private IStudentService studentService;
	private IProposalDocumentService proposalDocumentService;
	
	public IProposalDocumentService getProposalDocument() {
		return proposalDocumentService;
	}
	@Resource
	public void setProposalDocument(IProposalDocumentService proposalDocument) {
		this.proposalDocumentService = proposalDocument;
	}
	public IProposalDao getProposalDao() {
		return proposalDao;
	}
	public ITeacherService getTeacherService() {
		return teacherService;
	}
	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}
	
	public IStudentService getStudentService() {
		return studentService;
	}
	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}
	@Resource
	public void setProposalDao(IProposalDao proposalDao) {
		this.proposalDao = proposalDao;
	}
	@Override
	public List<Proposal> queryListByLoginId(String loginId,String groupId,String role) {
		return proposalDao.queryListByLoginId(loginId,groupId,role);
	}
	@Override
	public String checkRole(String userId) {
		Teacher teacher=teacherService.queryModel(userId);
		Student student=studentService.queryModel(userId);
		if(teacher!=null){
			return "teacher";
		}else if(student!=null){
			return "student";
		}
		return null;
	}
	
	@Override
	public boolean applyChange(Proposal proposal) {
		if(super.applyChange(proposal)){
			if(ModelStatus.update.equals(proposal.getModelStatus())){
				deleteProposalDocument(proposal.getProposalId());
			}
			List<ProposalDocument> proposalDocuments=getProposalDocumentList(proposal);
			if(proposalDocuments!=null&&proposalDocuments.size()>0){
				return proposalDocumentService.insertListEx(proposalDocuments)==proposalDocuments.size();
			}
			return true;
		}
		return false;
	}
	
	private boolean deleteProposalDocument(String proposalId) {
		return proposalDocumentService.deleteProposalDocumentByProposalId(proposalId);
	}
	private List<ProposalDocument> getProposalDocumentList(Proposal proposal){
		List<ProposalDocument> proposalDocuments=new ArrayList<ProposalDocument>();
		String documentIds=proposal.getDocumentIds();
		ProposalDocument proposalDocument=null;
		
		if(!StringUtils.isEmpty(documentIds)){
			String[] docIds=documentIds.split(",");
			for (String docId : docIds) {
				proposalDocument=new ProposalDocument();
				proposalDocument.setId(CommonUtils.getKeyId());
				proposalDocument.setProposalId(proposal.getProposalId());
				proposalDocument.setDocumentId(docId);
				proposalDocument.setModelStatus(ModelStatus.add);
				
				proposalDocuments.add(proposalDocument);
			}
		}
		return proposalDocuments;
	}
	@Override
	public int queryCount(ProposalSearch search, String loginId, String role) throws DaoBaseException {
		return proposalDao.queryCount(search, loginId, role);
	}
	@Override
	public int queryCountByProposalName(String proposalName, String loginId,
			String role) {
		return proposalDao.queryCount(proposalName, loginId, role);
	}
	@Override
	public List<Proposal> queryListByProposalName(String proposalName) {
		return proposalDao.queryListByProposalName(proposalName);
	}
}
